package com.app.ui.ui1;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;

import com.app.AspectApp;
import com.app.AspectApp.Paint;
import com.app.AspectApp.Scan;
import com.app.utils.Constants;

public class MethodTrackShowUI {

	private JFrame frame;
	public JTabbedPane tab;
	private static MethodTrackShowUI test = new MethodTrackShowUI();
	private Paint paint;
	private Scan scan;

	public static MethodTrackShowUI getInstance() {

		if (test != null)
			test = new MethodTrackShowUI();

		return test;
	}

	private MethodTrackShowUI() {
		initGUI();
	}

	private void initGUI() {

		frame = new JFrame();
		tab = new JTabbedPane(JTabbedPane.TOP);
		tab.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					int pos = tab.indexAtLocation(e.getX(), e.getY());
					if (pos != -1) {
						String title = tab.getTitleAt(pos);
						String callThread = AspectApp.getCallThread(title);
						if (callThread == null && title.equals("main"))
							callThread = Constants.MESSAGE_THIS_IS_MAIN_THREAD;
						else if (callThread == null)
							callThread = Constants.MESSAGE_NO_FATHER_THREAD;
						else
							callThread = Constants.MESSAGE_FATHER_THREAD + callThread;
						JOptionPane.showMessageDialog(frame, callThread, Constants.TITLE_THREAD_CALL, JOptionPane.INFORMATION_MESSAGE);
						
					}
				}
			}
		});

		frame.setTitle("");
		frame.getContentPane().add(tab);
		frame.getContentPane().revalidate();
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setSize(800, 600);
		frame.setVisible(false);

		frame.addWindowListener(new WindowListener() {

			@Override
			public void windowOpened(WindowEvent e) {
			}

			@Override
			public void windowIconified(WindowEvent e) {
			}

			@Override
			public void windowDeiconified(WindowEvent e) {
			}

			@Override
			public void windowDeactivated(WindowEvent e) {
			}

			@Override
			public void windowClosing(WindowEvent e) {
			}

			@Override
			public void windowClosed(WindowEvent e) {
				paint.stopThread();
				scan.stopThread();
			}

			@Override
			public void windowActivated(WindowEvent e) {
			}
		});
	}

	public void setScanThread(Scan scan) {
		this.scan = scan;
	}

	public void setPaintThread(Paint paint) {
		this.paint = paint;
	}

	public void addTab(String tabName, Component view) {
		tab.add(tabName, view);
	}

	public JTabbedPane getTab() {
		return tab;
	}

	public JFrame getJFrame() {
		return frame;
	}

	public void show() {
		paint.start();
		scan.start();
		frame.setVisible(true);
	}

	public void clearTab() {
		tab.removeAll();
	}
	
	public void setThreadStop(String thread) {
		int pos = tab.indexOfTab(thread);
		if (pos != -1)
			tab.setTitleAt(pos, thread + "(stop)");
	}
}
